package com.vvlive.caibao.smartcity.ui;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.ListView;

import com.vvlive.caibao.smartcity.adapter.AddFunctionAdapter;
import com.vvlive.caibao.smartcity.adapter.IndexFunctionAdapter;
import com.vvlive.caibao.smartcity.base.BaseActivity;
import com.vvlive.caibao.smartcity.bean.IndexFuncationListBean;
import com.vvlive.caibao.smartcity.utils.AddFunctionUtils;
import com.vvlive.caibao.smartcity.utils.FuncationInitUtils;
import com.vvlive.caibao.smartcity.view.CommonActionBar;

import org.kymjs.kjframe.ui.BindView;

import java.util.ArrayList;
import java.util.List;

import smartcity.vvlive.caibao.com.smartc.R;

/**
 * Created by lenovo on 2015/6/30.
 * 添加显示在主页显示列表的功能类
 */
public class AddActivity extends BaseActivity {
    @BindView(id = R.id.add_activity_bt,click = true)
    Button add_btn;
    ListView add_lv;
    AddFunctionAdapter mAddFunctionAdapter;
    String sign;
    String[] signs;
    //存放对应功能的activity类
    List<IndexFuncationListBean> mList = new ArrayList<IndexFuncationListBean>();
    IndexFunctionAdapter mIndexFunctionAdapter;
    List<IndexFuncationListBean> listAll;
    List<IndexFuncationListBean> list;
    boolean[] isC;
    @Override
    protected int getContentViewRes() {
        return R.layout.index_add_ft_activity;
    }

    @Override
    public void initWidget() {
        super.initWidget();
        CommonActionBar actionBar = super.getCommonActionBar();
        actionBar.setTitle("添加功能");
        add_lv = (ListView) findViewById(R.id.add_activity_lv);
        list = new ArrayList<>();
        listAll = new FuncationInitUtils().addAllFuncation();
        sign = getIntent().getExtras().getString("yd");
        Log.i("获取的列表",sign);
        signs = AddFunctionUtils.getStrs(sign, ",");
        for(int j=0;j<signs.length;j++){
            if("0".equals(signs[j])){
                list.add(listAll.get(j));
            }
        }
        isC = new boolean[list.size()];
        Log.i("list。size",list.size()+"    ");
        mAddFunctionAdapter = new AddFunctionAdapter(getApplicationContext(),list);
        add_lv.setAdapter(mAddFunctionAdapter);
        add_lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                //                Toast.makeText(getApplicationContext(),"长按"+position+"  sss:"+listAll.get(position).getSign(),Toast.LENGTH_SHORT).show();
                Log.i("移除", "长按"+position+"  sss:"+listAll.get(position).getSign());
                CheckBox ch = (CheckBox) view.findViewById(R.id.add_item_checkBox);
                if(ch.isChecked()){
                    ch.setChecked(false);
                    view.setBackgroundColor(Color.WHITE);
                    listAll.get(position).setIsShow(false);
                    signs[list.get(position).getSign()] = "0";
                    isC[position] = false;
                }else{
                    ch.setChecked(true);
                    view.setBackgroundColor(Color.rgb(185, 211, 238));
                    listAll.get(position).setIsShow(true);
                    signs[list.get(position).getSign()] = "1";
                    isC[position] = true;
                    //                    Toast.makeText(getApplicationContext(),""+position+"  sss:"+listAll.get(position).getSign(),Toast.LENGTH_SHORT).show();
                }
            }
        });
    }

    @Override
    public void widgetClick(View v) {
        super.widgetClick(v);
        switch (v.getId()){
            case R.id.add_activity_bt:
                Intent intent = new Intent();
                Bundle bundle = new Bundle();
                bundle.putString("add", AddFunctionUtils.getStr(signs, ","));
                bundle.putString("addX",selAdd());
                intent.putExtras(bundle);
                this.setResult(RESULT_OK, intent);
                finish();
                break;
        }
    }
    /**
     * 选择添加的项
     * */
    public String selAdd(){
        String s  = "";
        for(int i = 0;i<list.size();i++){
            if(isC[i]){
                s += ","+list.get(i).getSign();
                Log.i("将要返回的添加的","ssssssss===="+s);
            }
        }
        return s;
    }
}
